#!/bin/bash

# File ID: b8c0ee54-eb71-11df-a07a-fefdb24f8e10

progname=git-allbr
remotes=origin
test "$1" = "-a" && { remotes=$(git remote); shift; }
unset create_dangling
test "$1" = "-c" && { create_dangling=1; shift; }
unset force_str
test "$1" = "-f" && { force_str="--force"; shift; }
test -z "$1" || remotes="$@"
echo
is_bare=1
test "$(git config --get core.bare)" = "true" || is_bare=0
for remote in $remotes; do
    echo ==== $progname: remote $remote
    for f in $(git branch -a | grep remotes/$remote/ | grep -v -E "remotes/$remote/HEAD( |\$)" | perl -pe "s!remotes/$remote/(.*)\$!\$1!"); do
        test "$create_dangling" != "1" && echo $f | grep -qE '^commit-[0-9a-f]{40}$' && test "$is_bare" = "0" && continue
        (git branch --no-track $f $remote/$f 2>&1 && echo $progname: Created $f) | grep -v '^fatal: A branch named .* already exists.'
        git push . $remote/$f:refs/heads/$f $force_str 2>&1 | grep -v '^Everything up-to-date'
    done
    echo
done
